package cn.afactory.tygz.ui.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.PrivateUserInfo;
import cn.afactory.tygz.ui.base.entity.MessageInfo;
import cn.afactory.tygz.ui.base.utils.DateUtil;

/**
 * Created by Kenneth on 15/9/12.
 */
public class MessageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<PrivateUserInfo> mData = new ArrayList<PrivateUserInfo>();

    public MessageListAdapter(Context context, List<PrivateUserInfo> list) {
        mContext = context;
        mData = list;
    }

    //重新设置数据
    public void setData(List<PrivateUserInfo> list){
        mData = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mData != null ? mData.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        ImageView messageIcon;
        TextView messageContent;
        TextView unReadMessageCount;
        TextView messageTitle;
        TextView messageDate;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fg_message_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.messageIcon = (ImageView)convertView.findViewById(R.id.message_icon);
            viewHolder.messageTitle = (TextView)convertView.findViewById(R.id.message_title);
            viewHolder.messageContent = (TextView)convertView.findViewById(R.id.message_content);
            viewHolder.unReadMessageCount = (TextView)convertView.findViewById(R.id.unread_message_count);
            viewHolder.messageDate = (TextView)convertView.findViewById(R.id.message_date);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        PrivateUserInfo privateUserInfo = mData.get(position);
        if(privateUserInfo.msg != null) {
            viewHolder.messageContent.setText(privateUserInfo.msg);
        }

        if(privateUserInfo.nickName != null) {
            viewHolder.messageTitle.setText(privateUserInfo.nickName);
        }

        if(privateUserInfo.unRead > 0){
            viewHolder.unReadMessageCount.setVisibility(View.VISIBLE);
            viewHolder.unReadMessageCount.setText(privateUserInfo.unRead + "");
        }else{
            viewHolder.unReadMessageCount.setVisibility(View.GONE);
        }

        viewHolder.messageDate.setText(DateUtil.getTimeDescription(new Date(privateUserInfo.addTime)));

        if (privateUserInfo.userIcon != null) {
            BaseImgView.loadimg(viewHolder.messageIcon,
                    privateUserInfo.userIcon,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30), 0);
        } else {
            viewHolder.messageIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }
        return convertView;
    }
}
